﻿#include "homewidget.h"
#include "widgets/ulabel.h"
#include "widgets/ucirclebutton.h"
#include "widgets/upushbutton.h"
#include "uapplication.h"

#include <QDesktopServices>
#include <QHBoxLayout>

HomeWidget::HomeWidget(QWidget* parent) : BaseWidget(parent)
{
    init();
}

HomeWidget::~HomeWidget()
{
}

QVBoxLayout *HomeWidget::mainLayout()
{
    return m_mainLayout;
}

void HomeWidget::initUI()
{
    QWidget* mainWidget = new QWidget(this);
    m_mainLayout = new QVBoxLayout;
    m_mainLayout->setContentsMargins(0, 0, 0, 0);
    mainWidget->setLayout(m_mainLayout);

    ULabel* label = new ULabel(this);
    label->setSizePolicy(QSizePolicy::Policy::Expanding, QSizePolicy::Policy::Expanding);
    label->setLabelPixmap(QPixmap(":/icons/backgroud.png"));
    QVBoxLayout* lableLayout = new QVBoxLayout;
    label->setLayout(lableLayout);

    ULabel* titleLabel = new ULabel("Union Toolkit", this);
    titleLabel->setFontColor(QColor("#C0C6D4"));
    titleLabel->setIndent(8);
    titleLabel->setTextSize(26);
    titleLabel->setFontWeight(QFont::Bold);

    UWidget* widget = new UWidget(this);
    widget->setMargins(8);
    widget->addDropShadowEffect(true);
    widget->setBackgroundRole(QPalette::Base);
    widget->setRadius(8);
    QVBoxLayout* layout = new QVBoxLayout;
    layout->setContentsMargins(18, 18, 18, 18);
    widget->setLayout(layout);
    widget->setFixedSize(260, 280);

    ULabel* nameLabel = new ULabel("UTK Gitee", this);
    nameLabel->setFontWeight(QFont::Bold);
    introductionLabel = new ULabel(tr("UTK (Union Tool Kit) is a simple and practical universal development framework platform developed based on Qt, which helps developers quickly carry out project development"), this);
    introductionLabel->setWordWrap(true);
    linkBtn = new UPushButton(this);
    linkBtn->setButtonType(ButtonType::ButtonNoBackground);
    linkBtn->setIcon(QIcon::fromTheme("utk_link"));
    auto logo = new UCircleButton(QPixmap(":/icons/gitee_logo.jpg"), this);
    logo->setCheckable(false);
    layout->addWidget(logo);
    layout->addSpacing(20);
    layout->addWidget(nameLabel);
    layout->addWidget(introductionLabel);
    layout->addStretch();
    layout->addWidget(linkBtn, 0, Qt::AlignRight);

    lableLayout->addWidget(titleLabel);
    lableLayout->addWidget(widget);

    m_mainLayout->addWidget(label);

    setWidget(mainWidget);
}

void HomeWidget::initConnection()
{
    connect(linkBtn,  &UPushButton::clicked, this, [ = ]()
    {
        QDesktopServices::openUrl(uApp->organizationDomain());
            });
}

void HomeWidget::updateLanguage()
{
    introductionLabel->setText(tr("UTK (Union Tool Kit) is a simple and practical universal development framework platform developed based on Qt, which helps developers quickly carry out project development"));
}
